home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / random.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  17KB  |  562 lines

  1. /*
  2.  * This file contains the
  3.  * command processing functions for
  4.  * a number of random commands. There is
  5.  * no functional grouping here, for
  6.  * sure.
  7.  */
  8. #include        <stdio.h>
  9. #include        "ed.h"
  10.  
  11. int     tabsize;                        /* Tab size (0: use real tabs)  */
  12. char    fillprefix[80];                 /* rather large but who knows   */
  13. int     yankcflag = FALSE;
  14.  
  15. /*
  16.  * Set fill column to n.  The fill column is global, but the individual
  17.  * buffer can be filled or not depending on its mode.
  18.  */
  19. setfillcol(f, n)
  20. register int f, n;
  21. {
  22.         register WINDOW *wp;
  23.         register BUFFER *bp;
  24.  
  25.         fillcol = (n == 1) ? (getccol(FALSE) + 1) : n;
  26.         n = fillcol;
  27.         wp =  wheadp;
  28.         bp =  curwp->w_bufp;
  29.         if (n > 1)
  30.                 {
  31.                 bp->b_bmode |= BMWRAP;
  32.                 bp->b_bmode &= ~BMNWRAP;
  33.                 }
  34.         else
  35.                 {
  36.                 bp->b_bmode |= BMNWRAP;
  37.                 bp->b_bmode &= ~BMWRAP;
  38.                 }
  39.         upmode();
  40.         mlwrite("Fill column is %d", n);
  41.         return(TRUE);
  42. }
  43.  
  44. /*
  45.  * Set indent column to n or set fill prefix to string.
  46.  */
  47. setindcol(f, n)
  48. register int f, n;
  49. {
  50.         short curdo;    /* current dot offset */
  51.         register short tdo;     /* moveable dot offset */
  52.         register short i;
  53.         char tstring[80];       /* temporary store for prefix string */
  54.         i = 0;
  55.         tdo = 0;
  56.         curdo = curwp->w_doto;
  57.  
  58.         if (n>1)        /* called with argument, so just set the indcol */
  59.                 {
  60.                 if (n >= 80)
  61.                         n=79;
  62.                 indcol = n;
  63.                 /* copy the correct number of tabs and spaces into string */
  64.                 if ((tdo=indcol/8)!=0)
  65.                         while (tdo--)
  66.                                 tstring[i++] = '\t';
  67.                 if ((tdo=indcol%8)!=0)
  68.                         while (tdo--)
  69.                                 tstring[i++] =  ' ';
  70.                 tstring[i] = '\0';
  71.                 strncpy(fillprefix,tstring,79);
  72.                 mlwrite("Indent column is %d", indcol);
  73.                 return(TRUE);
  74.                 }
  75.         indcol = getccol(FALSE) +1;
  76.         /* now fabricate fill prefix string */
  77.         do {
  78.                 curwp->w_doto = tdo;
  79.                 tstring[tdo] = lgetc(curwp->w_dotp,tdo);
  80.         } while (++tdo < curdo && tdo < 80);
  81.         tstring[curdo] = '\0';
  82.         curwp->w_doto = curdo;
  83.         strncpy(fillprefix,tstring,79);
  84.         mlwrite("Fill prefix is '%s'",fillprefix);
  85.         return(TRUE);
  86. }
  87.  
  88. /*
  89.  * Display the current position of the cursor,
  90.  * in origin 1 X-Y coordinates, the character that is
  91.  * under the cursor (in hex), and the fraction of the
  92.  * text that is before the cursor. The displayed column
  93.  * is not the current column, but the column that would
  94.  * be used on an infinite width display. Normally this
  95.  * is bound to "C-X =".
  96.  */
  97. showcpos(f,n)
  98. int f, n;
  99. {
  100.         register LINE   *clp;
  101.         register long   nch;
  102.         register long   nbc;
  103.         register long   nmc;
  104.         register int    cbo;
  105.         register int    cac;
  106.         register int    page;
  107.         register int    col;
  108.         register int    i;
  109.         register int    row;
  110.         register long   tl1;
  111.         float    ratio, tf1, tf2;
  112.         char     buf[12], *ftoa();
  113.  
  114.         i = 0;
  115.         clp = lforw(curbp->b_linep);            /* Grovel the data.     */
  116.         cbo = 0;
  117.         nch = 0;
  118.         for (;;) {
  119.                 if (clp==curwp->w_markp && cbo==curwp->w_marko)
  120.                         nmc = nch;
  121.                 if (clp==curwp->w_dotp && cbo==curwp->w_doto) {
  122.                         nbc = nch;
  123.                         row = i;
  124.                         if (cbo == llength(clp))
  125.                                 cac = '\n';
  126.                         else
  127.                                 cac = lgetc(clp, cbo);
  128.                 }
  129.                 if (cbo == llength(clp)) {
  130.                         if (clp == curbp->b_linep)
  131.                                 break;
  132.                         clp = lforw(clp);
  133.                         ++i;
  134.                         cbo = 0;
  135.                 } else
  136.                         ++cbo;
  137.                 ++nch;
  138.         }
  139.         col = getccol(FALSE) + 1;               /* Get real column.     */
  140.         if (nch != 0L)
  141.                 {
  142.                 tl1 = nbc * 100L;
  143.                 tf1 = (float)tl1;
  144.                 tf2 = (float)nch;
  145.                 ratio = tf1/tf2;
  146.                 }
  147.         (void)ftoa(ratio,buf,4);
  148.         buf[strlen(buf)-2] = '\0';      /* fool precision error */
  149.         if (curwp->w_dotp == curbp->b_linep)
  150.                 row = i;
  151.         page = row / pagelen + 1;
  152. mlwrite("page:=%d col:=%d line:=%d CH:=0x%x mark:=%D point:=%D (%s%% of %D)",
  153.                  page, col, row, cac, nmc, nbc, buf, nch);
  154.         return (TRUE);
  155. }
  156.  
  157. /*
  158.  * Return current column.  Stop at first non-blank given TRUE argument.
  159.  */
  160. getccol(bflg)
  161. int bflg;
  162. {
  163.         register int c, i, col;
  164.         col = 0;
  165.         for (i=0; i<curwp->w_doto; ++i) {
  166.                 c = lgetc(curwp->w_dotp, i);
  167.                 if (c!=' ' && c!='\t' && bflg)
  168.                         break;
  169.                 if (c == '\t')
  170.                         col |= 0x07;
  171.                 else if (c<0x20 || c==0x7F)
  172.                         ++col;
  173.                 ++col;
  174.         }
  175.         return(col);
  176. }
  177.  
  178. /*
  179.  * Twiddle the two characters on either side of
  180.  * dot. If dot is at the end of the line twiddle the
  181.  * two characters before it. Return with an error if dot
  182.  * is at the beginning of line; it seems to be a bit
  183.  * pointless to make this work. This fixes up a very
  184.  * common typo with a single stroke. Normally bound
  185.  * to "C-T". This always works within a line, so
  186.  * "WFEDIT" is good enough.
  187.  */
  188. twiddle(f, n)
  189. register int f, n;
  190. {
  191.         register LINE   *dotp;
  192.         register int    doto;
  193.         register int    cl;
  194.         register int    cr;
  195.  
  196.         dotp = curwp->w_dotp;
  197.         doto = curwp->w_doto;
  198.         if (doto==llength(dotp) && --doto<0)
  199.                 return (FALSE);
  200.         cr = lgetc(dotp, doto);
  201.         if (--doto < 0)
  202.                 return (FALSE);
  203.         cl = lgetc(dotp, doto);
  204.         lputc(dotp, doto+0, cr);
  205.         lputc(dotp, doto+1, cl);
  206.         lchange(WFEDIT);
  207.         return (TRUE);
  208. }
  209.  
  210. /*
  211.  * Quote the next character, and
  212.  * insert it into the buffer. All the characters
  213.  * are taken literally, with the exception of the newline,
  214.  * which always has its line splitting meaning. The character
  215.  * is always read, even if it is inserted 0 times, for
  216.  * regularity. Bound to "M-Q" (for me) and "C-Q" (for Rich,
  217.  * and only on terminals that don't need XON-XOFF).
  218.  */
  219. quote(f, n)
  220. register int f, n;
  221. {
  222.         register int    s;
  223.         register int    c;
  224.  
  225.         c = (*term.t_getchar)();
  226.         if (n < 0)
  227.                 return (FALSE);
  228.         if (n == 0)
  229.                 return (TRUE);
  230.         if (c == '\n') {
  231.                 do {
  232.                         s = lnewline();
  233.                 } while (s==TRUE && --n);
  234.                 return (s);
  235.         }
  236.         return (linsert(n, c));
  237. }
  238.  
  239. /*
  240.  * Set tab size if given non-default argument (n <> 1).  Otherwise, insert a
  241.  * tab into file.  If given argument, n, of zero, change to true tabs.
  242.  * If n > 1, simulate tab stop every n-characters using spaces.
  243.  * This has to be done in this slightly funny way because the
  244.  * tab (in ASCII) has been turned into "C-I" (in 10
  245.  * bit code) already. Bound to "C-I".
  246.  */
  247. tab(f, n)
  248. register int f, n;
  249. {
  250.         if (n < 0)
  251.                 return (FALSE);
  252.         if (n == 0 || n > 1) {
  253.                 tabsize = n;
  254.                 return(TRUE);
  255.         }
  256.         if (! tabsize)
  257.                 return(linsert(1, '\t'));
  258.         return(linsert(tabsize - (getccol(FALSE) % tabsize), ' '));
  259. }
  260.  
  261. /*
  262.  * Open up some blank space. The basic plan
  263.  * is to insert a bunch of newlines, and then back
  264.  * up over them. Everything is done by the subcommand
  265.  * processors. They even handle the looping. Normally
  266.  * this is bound to "C-O".
  267.  */
  268. openline(f, n)
  269. register int f, n;
  270. {
  271.         register int    i;
  272.         register int    s;
  273.  
  274.         if (n < 0)
  275.                 return